package com.lai.qq.util.utils;

import lombok.extern.slf4j.Slf4j;

import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * @author xss <xss, 364647220@qq.com>
 * @version v1.0
 * @Description
 * @encoding UTF-8
 * @date 2020/8/13
 * @time 11:54
 * @修改记录 <pre>
 * 版本       修改人         修改时间         修改内容描述
 * --------------------------------------------------
 * <p>
 * --------------------------------------------------
 * </pre>
 */
@Slf4j
public class Date8Util {

    public static final DateTimeFormatter DATETIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    public static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DATE_FORMATTER_YY = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static final DateTimeFormatter DATE_FORMATTER_MM = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");

    public static String DateToString(Date date, String formatter) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(formatter);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.ofInstant(instant, zoneId);
        return dateTimeFormatter.format(localDateTime);
    }

    public static Date stringToDate(LocalDateTime date, String appointMnet) {
        StringBuilder stringBuilder = new StringBuilder();
        //.append(date.getSecond())
        String str1 = stringBuilder.append(date.getYear()).append("-").append(date.getMonthValue()).append("-")
                .append(date.getDayOfMonth()).append(" ").append(appointMnet.split(":")[0]).append(":").append(appointMnet.split(":")[1]).append(":00").toString();
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-M-d HH:mm:ss");
        LocalDateTime parse = LocalDateTime.parse(str1, dtf);
        return Date.from(parse.atZone(ZoneId.systemDefault()).toInstant());
    }

    public static Date localDateTimeToDate(LocalDateTime date) {
        ZoneId zoneId = ZoneId.systemDefault();
        ZonedDateTime zdt = date.atZone(zoneId);
        return Date.from(zdt.toInstant());
    }

    /**
     * 格式化 LocalDate
     *
     * @param localDate 将要格式化的日期
     * @return 例如,  2020-04-03
     */
    public static String localDateToString(LocalDate localDate) {
        return localDate == null ? null : DATE_FORMATTER.format(localDate);
    }

    /**
     * 获取上个月当天的日期格式
     *
     * @return
     */
    public static Date getLastMonthTime() {
        LocalDateTime date = LocalDateTime.now();
        date = date.minusMonths(1);
        return localDateTimeToDate(date);
    }

    /**
     * 获取上个月当天的yy-mm-dd格式
     *
     * @return
     */
    public static String getLastMonthDay() {
        LocalDate date = LocalDate.now();
        date = date.minusMonths(1);
        return localDateToString(date);
    }

    /**
     * 获取上个月当天的后几天的日期的yy-mm-dd格式
     *
     * @return
     */
    public static String getLastMonthBackDay(int i) {
        LocalDate date = LocalDate.now();
        date = date.minusMonths(1);
        return localDateToString(date.plusDays(i));
    }



    /**
     * 日期相隔天数
     *
     * @param startDateInclusive
     * @param endDateExclusive
     * @return
     */
    public static int periodDays(LocalDate startDateInclusive, LocalDate endDateExclusive) {
        return Period.between(startDateInclusive, endDateExclusive).getDays();
    }

    /**
     * 当月现在距离月初多少天
     *
     * @return
     */
    public static int periodDaysMowToFirst() {
        return Period.between(getFirstDay(), getCurrentLocalDate()).getDays();
    }

    /**
     * 日期相隔小时
     *
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long durationHours(Temporal startInclusive, Temporal endExclusive) {
        return Duration.between(startInclusive, endExclusive).toHours();
    }

    /**
     * 日期相隔分钟
     *
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long durationMinutes(Temporal startInclusive, Temporal endExclusive) {
        return Duration.between(startInclusive, endExclusive).toMinutes();
    }

    /**
     * 日期相隔毫秒数
     *
     * @param startInclusive
     * @param endExclusive
     * @return
     */
    public static long durationMillis(Temporal startInclusive, Temporal endExclusive) {
        return Duration.between(startInclusive, endExclusive).toMillis();
    }

    /**
     * 是否当天
     *
     * @param date
     * @return
     */
    public static boolean isToday(LocalDate date) {
        return getCurrentLocalDate().equals(date);
    }

    /**
     * 获取本月的第一天
     *
     * @return
     */
    public static LocalDate getFirstDay() {
        return getCurrentLocalDate().with(TemporalAdjusters.firstDayOfMonth());
    }

    /**
     * 获取本月的第一天
     *
     * @return
     */
    public static String getFirstDayOfThisMonth() {
        return getCurrentLocalDate().with(TemporalAdjusters.firstDayOfMonth()).format(DATE_FORMATTER);
    }

    /**
     * 获取本月的最后一天
     *
     * @return
     */
    public static String getLastDayOfThisMonth() {
        return getCurrentLocalDate().with(TemporalAdjusters.lastDayOfMonth()).format(DATE_FORMATTER);
    }

    /**
     * 返回当前的日期
     *
     * @return
     */
    public static LocalDate getCurrentLocalDate() {
        return LocalDate.now();
    }

    public static String getCurrentMonth() {
        return LocalDate.now().format(DATE_FORMATTER_YY);
    }

    public static void main(String[] args) {

        System.out.println(getLastMonthBackDay(2));

        System.out.println(
                LocalDateTime.now().format(DateTimeFormatter
                        .ofPattern("yyyy-MM-dd HH:mm"))
        );
    }

}
